Leetcode 66. Plus One

文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

1. Description

Plus One

2. Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
public:
vector<int> plusOne(vector<int>& digits) {
int length = digits.size();
int index = length - 1;
while(index >= 0) {
int sum = digits[index] + 1;
if(sum == 10) {
digits[index] = 0;
index--;
}
else {
digits[index] = sum;
return digits;
}
}
digits.insert(digits.begin(), 1);
return digits;
}
};

Reference

  1. https://leetcode.com/problems/plus-one/description/
如果有收获,可以请我喝杯咖啡!